﻿using Pub;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RainMgr
{
    public partial class FOpt_zy : Form
    {
        ADOTest ado1;
        public FOpt_zy()
        {
            InitializeComponent();
        }

        private void FOpt_zy_Load(object sender, EventArgs e)
        {
            ado1 = new ADOTest();
            ado1.S_Con = CConfig.getPermite("DBCon");
            // 站点维护
            string strSQL = "SELECT * FROM P_Unit";
            DataSet ds1 = ado1.sel1(strSQL);
            listBox1.DataSource = ds1.Tables[0];
            listBox1.DisplayMember = "UNAME";
            listBox1.ValueMember = "UID";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.Text == string.Empty) return;
            String UID = listBox1.SelectedValue.ToString();
            TreeNodeCollection nodes = treeView1.Nodes;
            nodes.Clear();
            fillMenuInfo("0", UID, nodes);
        }

        private void fillMenuInfo(string PreID, string UID, TreeNodeCollection nodes)
        {
            //CDebug.EnterMethod("刷新雨情站点");
            try
            {
                string strSQL = String.Format("SELECT * FROM P_Pf_Task WHERE PreID='{0}' ORDER BY OrderID", PreID);
                DataSet ds1 = ado1.sel1(strSQL);
                //CDebug.Send(strSQL + "\n--返回结果个数: " + ado1.ds_count);
                if (!CDB.dsIsNull(ds1))
                {
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string name = dr1["M_Name"].ToString();
                        string ID = dr1["ID"].ToString();
                        string GrantPer = dr1["GrantPer"].ToString();
                        TreeNode node1 = new TreeNode();
                        node1.Text = name;
                        node1.Tag = ID;
                        node1.ToolTipText = GrantPer;
                        if (GrantPer.IndexOf("#" + UID + "#") != -1)
                            node1.Checked = true;

                        /*
                        //填充站点名称
                        string strSQL2 = String.Format("SELECT * FROM P_Pf_Task WHERE PreID='{0}' ORDER BY OrderID", ID);
                        DataSet ds2 = ado1.sel1(strSQL2);
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            DataRow dr2 = ds2.Tables[0].Rows[j];
                            string name2 = dr2["M_Name"].ToString();
                            string ID2 = dr2["ID"].ToString();
                            string GrantPer2 = dr2["GrantPer"].ToString();
                            TreeNode node2 = new TreeNode();
                            node2.Text = name2;
                            node2.Tag = ID2;
                            node2.ToolTipText = GrantPer2;
                            if (GrantPer2.IndexOf("#" + UID + "#") != -1) node2.Checked = true;

                            node1.Nodes.Add(node2);
                        }
                        */
                        fillMenuInfo(ID, UID, node1.Nodes);

                        node1.Expand();
                        nodes.Add(node1);
                    }
                }

            }
            catch (System.Exception ex)
            {
                //CDebug.SendError(ex.ToString());
            }
            //CDebug.ExitMethod("刷新雨情站点");
        }

        private void btn_UserOpt_Click(object sender, EventArgs e)
        {
            try
            {
                if (listBox1.Text == string.Empty) return;
                String uid = listBox1.SelectedValue.ToString();
                updateMenu(uid, treeView1.Nodes);
                /*
                foreach (TreeNode node1 in treeView1.Nodes)
                {
                    String id = node1.Tag.ToString();
                    String gp = node1.ToolTipText;
                    Boolean b_edit = false;
                    if (gp.IndexOf("#" + uid + "#") != -1 && !node1.Checked)
                    {
                        gp = gp.Replace("#" + uid + "#", "#");
                        b_edit = true;
                    }
                    else if (gp.IndexOf("#" + uid + "#") == -1 && node1.Checked)
                    {
                        gp += uid + "#";
                        b_edit = true;
                    }

                    if (b_edit)
                    {
                        string strSQL = string.Format("UPDATE P_Pf_Task SET GrantPer='{0}' WHERE ID={1}", gp, id);
                        ado1.cmd1(strSQL);
                    }

                }*/
                MessageBox.Show("保存成功!");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        }

        private void updateMenu(String uid, TreeNodeCollection nodes) {
            foreach (TreeNode node1 in nodes)
            {
                String id = node1.Tag.ToString();
                String gp = node1.ToolTipText;
                Boolean b_edit = false;
                if (gp.IndexOf("#" + uid + "#") != -1 && !node1.Checked)
                {
                    gp = gp.Replace("#" + uid + "#", "#");
                    b_edit = true;
                }
                else if (gp.IndexOf("#" + uid + "#") == -1 && node1.Checked)
                {
                    gp += uid + "#";
                    b_edit = true;
                }

                if (b_edit)
                {
                    string strSQL = string.Format("UPDATE P_Pf_Task SET GrantPer='{0}' WHERE ID={1}", gp, id);
                    ado1.cmd1(strSQL);
                }
                updateMenu(uid, node1.Nodes);
            }
        }
    }
}
